package com.example.videoconverter;

import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;

import java.io.File;

/**
 * 简化的视频转换器
 * 包含独立的simpleConvertAviToMp4方法和main方法
 */
public class SimpleVideoConverter {
    
    /**
     * 简化的AVI转MP4方法（不依赖JavaCV）
     * 使用JAVE库进行转换，设置默认编码参数
     * 
     * @param inputPath 输入AVI文件路径
     * @param outputPath 输出MP4文件路径
     * @throws EncoderException 转换过程中的异常
     */
    public void simpleConvertAviToMp4(String inputPath, String outputPath) throws EncoderException {
        File source = new File(inputPath);
        File target = new File(outputPath);
        
        // 创建音频属性
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("aac");
        audio.setBitRate(128000); // 128 kbps
        audio.setChannels(2);
        audio.setSamplingRate(44100);
        
        // 创建视频属性
        VideoAttributes video = new VideoAttributes();
        video.setCodec("libx264");
        video.setBitRate(1500000); // 1.5 Mbps
        video.setFrameRate(30);
        
        // 创建编码属性
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setOutputFormat("mp4");
        attrs.setAudioAttributes(audio);
        attrs.setVideoAttributes(video);
        
        // 执行转换
        Encoder encoder = new Encoder();
        encoder.encode(new MultimediaObject(source), target, attrs);
    }
    
    /**
     * 主方法，用于测试simpleConvertAviToMp4方法
     */
    public static void main(String[] args) {
        SimpleVideoConverter converter = new SimpleVideoConverter();
        
        // 测试文件路径
        String inputPath = "target/classes/sample_1280x720.avi";
        String outputPath = "target/simple_converted.mp4";
        
        try {
            System.out.println("=== 简化AVI转MP4转换测试 ===");
            System.out.println("输入文件: " + inputPath);
            System.out.println("输出文件: " + outputPath);
            
            // 检查输入文件是否存在
            File inputFile = new File(inputPath);
            if (!inputFile.exists()) {
                System.out.println("错误: 输入文件不存在: " + inputPath);
                System.out.println("请确保项目中有测试视频文件");
                return;
            }
            
            System.out.printf("输入文件大小: %.2f MB%n", inputFile.length() / (1024.0 * 1024.0));
            
            System.out.println("\n开始转换...");
            long startTime = System.currentTimeMillis();
            
            // 调用转换方法
            converter.simpleConvertAviToMp4(inputPath, outputPath);
            
            long endTime = System.currentTimeMillis();
            double duration = (endTime - startTime) / 1000.0;
            
            // 验证输出文件
            File outputFile = new File(outputPath);
            if (outputFile.exists()) {
                System.out.println("\n✅ 转换成功!");
                System.out.printf("转换耗时: %.2f 秒%n", duration);
                System.out.printf("输出文件大小: %.2f MB%n", outputFile.length() / (1024.0 * 1024.0));
                System.out.printf("压缩比: %.2f%%%n", (double) outputFile.length() / inputFile.length() * 100);
                System.out.println("输出文件路径: " + outputFile.getAbsolutePath());
            } else {
                System.out.println("\n❌ 转换失败: 输出文件未生成");
            }
            
        } catch (EncoderException e) {
            System.out.println("\n❌ 转换过程中发生错误:");
            System.out.println("错误信息: " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("\n❌ 发生未知错误:");
            System.out.println("错误信息: " + e.getMessage());
            e.printStackTrace();
        }
    }
}